import type { TableColumn } from '@/components/core/dynamic-table';
import { type TreeDataItem } from '@/core/permission/utils';

export type TableListItem = API.FormSortListResultItem;
export type TableColumnItem = TableColumn<TableListItem>;

export const baseColumns: TableColumnItem[] = [
  {
    title: '类型名称',
    dataIndex: 'name',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
  {
    title: '类型编号',
    dataIndex: 'id',
    align: 'center',
    hideInSearch: true,
    width: 160,
  },
];

export const formatFormSortTree = (
  depts: API.FormSortListResultItem[],
  parentNo: number | string = 0,
  keyPath: (string | number)[] = [],
): TreeDataItem[] => {
  return depts
    .filter((item) => item.parentNo === parentNo)
    .map((item) => {
      const _keyPath = keyPath.concat(parentNo || []);
      const arr = formatFormSortTree(depts, item.id, _keyPath);
      return Object.assign(item, {
        keyPath: _keyPath,
        title: item.name,
        key: item.id,
        value: item.id,
        formData: item,
        children: arr.length ? arr : null,
      });
    });
};
